package com.libui.navigatonbar.menuview

import android.content.Context
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.view.setPadding
import com.libui.R
import com.libui.TextViewEx
import com.libui.navigatonbar.MenuData
import com.libui.navigatonbar.OnMenuTap
import razerdp.basepopup.BasePopupWindow

class PopMenuView(
    context: Context,
    menus: List<MenuData>,
    var onMenuTap: OnMenuTap? = null,
) : BasePopupWindow(context) {
    init {
        setContentView(R.layout.pop_menu_view)

        val linearLayout = findViewById<LinearLayout>(R.id.pop_menu_view)

        menus.forEach { menuData ->
            val textViewEx = TextViewEx(context)
            val layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                context.resources.getDimensionPixelSize(R.dimen.right_menu_pop_view_item_height)
            )
            textViewEx.text = menuData.title
            textViewEx.setTextColor(context.resources.getColor(R.color.pop_menu_item_title, null))
            textViewEx.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
            textViewEx.gravity = Gravity.CENTER_VERTICAL
            textViewEx.setPadding(context.resources.getDimensionPixelSize(R.dimen.right_menu_pop_view_item_padding))

            textViewEx.setOnClickListener {
                onMenuTap?.let { it((menuData)) }
            }

            linearLayout.addView(textViewEx, layoutParams)
        }
    }
}